package com.ls.dbpool;

import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TableNameClassify {
	public static final String DAY = "day";
	public static final String HOUR = "hour";
	public static final Pattern hourPattern = Pattern.compile("(cc_log_.*_)(\\d{2}$)");
	public static final Pattern dayPattern = Pattern.compile("(cc_log_.*_)(dd_\\d{8}$)");
/**
 * 不依赖表名顺序
 * @param tableNames
 * @return
 */
	public static Map<String, List<List<String>>> classifyTable(List<String> tableNames) {
		List<List<String>> dayList = new LinkedList<List<String>>();
		List<List<String>> hourList = new LinkedList<List<String>>();
		Map<String, List<List<String>>> map = new HashMap<String, List<List<String>>>(2);
		map.put(DAY, dayList);
		map.put(HOUR, hourList);
		Map<String,List<String>> hourTypeMap=new HashMap<String,List<String>>();
		Map<String,List<String>> dayTypeMap=new HashMap<String,List<String>>();
		if (tableNames == null || tableNames.isEmpty())
			return map;
		for (String name : tableNames) {
			if(classifyName(name,hourPattern,hourTypeMap))
				continue;
			classifyName(name,dayPattern,dayTypeMap);
		}
		hourList.addAll(hourTypeMap.values());
		dayList.addAll(dayTypeMap.values());
		orderHourListByCurrentTime(hourList);
		return map;
	}
	/**
	 * 按照距离当前时间远近排序表
	 * 
	 * @param hourList
	 */
	private static void orderHourListByCurrentTime(List<List<String>> hourList) {
		Calendar c = Calendar.getInstance();
		final int hour = c.get(Calendar.HOUR_OF_DAY);
		for (List<String> hList : hourList) {
			Collections.sort(hList, new Comparator<String>() {
				public int compare(String x, String y) {
					Matcher m1 = hourPattern.matcher(x);
					Matcher m2 = hourPattern.matcher(y);
					if (m1.matches() && m2.matches()) {
						int h1 = Integer.valueOf(m1.group(2));
						int h2 = Integer.valueOf(m2.group(2));
						h1 = (h1 - hour - 1 + 24) % 24;
						h2 = (h2 - hour - 1 + 24) % 24;
						return h1 > h2 ? 1 : h1 == h2 ? 0 : -1;
					}
					return 0;
				}
			});
		}
	}
	private static  boolean classifyName(String name,Pattern pattern,Map<String,List<String>> typeMap){
		Matcher match;
		String prefix;
		match = pattern.matcher(name);
		if (match.matches()) {
			prefix = match.group(1);
			List<String> logNameList = typeMap.get(prefix);
			if(logNameList==null){
				logNameList = new LinkedList<String>();
				typeMap.put(prefix, logNameList);
			}
			logNameList.add(name);
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		// Pattern p=Pattern.compile("(cc_log\\_.*\\_)(\\d{2}$)");
		Pattern p2 = Pattern.compile("(cc_log\\_.*\\_)(dd\\_\\d{8}$)");
		Matcher m1 = p2.matcher("cc_log_app_nc_mwsummary_dd_20161104");
		// Matcher m2 = p.matcher("cc_log_app_nc_mwsummary_22");
		System.out.println(m1.matches());
		if (m1.matches()) {
			System.out.println(m1.group());
			System.out.println(m1.group(1));
			System.out.println(m1.group(2));
		}

	}
}
